package cn.cloud.butler.webapi.service;

import cn.cloud.butler.webapi.domain.AppVersionEntity;
import cn.cloud.butler.webapi.domain.CompanyEntity;
import cn.cloud.butler.webapi.domain.DownloadPictureEntity;
import cn.cloud.butler.webapi.logger.WaLogger;
import cn.cloud.butler.webapi.repository.AppversionRepository;
import cn.cloud.butler.webapi.repository.CompanyRepository;
import cn.cloud.butler.webapi.repository.DownloadPictureRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * checkApk业务逻辑层
 */

@Service
public class AboutApkService {


    private final String URL_BASE = "https://asset.hzncc.cn/HncWeb";

    private final AppversionRepository appversionRepository;
    private final CompanyRepository companyRepository;
    private final DownloadPictureRepository downloadPictureRepository;
    @Autowired
    public AboutApkService(AppversionRepository appversionRepository,
                           CompanyRepository companyRepository,
                           DownloadPictureRepository downloadPictureRepository){
        this.appversionRepository =appversionRepository;
        this.companyRepository = companyRepository;
        this.downloadPictureRepository = downloadPictureRepository;
    }


    /**
     * 版本更新检测
     * @param versionnum 当前apk版本号
     * @param identify 当前apk版本类型
     * @return 版本更新检测
     */
    @Cacheable(value = "user-key")
    public HashMap<String,Object> checkUpdateApk(Long versionnum, String identify){
        Integer MsgCode;
        HashMap<String,Object> result = new HashMap<>();
        AppVersionEntity appVersionEntity = appversionRepository.findNewestByIdentify(identify);
        if (appVersionEntity == null) {
            result.put("MsgCode","没有最新版本");
            result.put("MsgDesc",-3);
            result.put("downUrl",null);
            return result;
        }
        MsgCode = appVersionEntity.getVerNum().intValue();
        if (versionnum < appVersionEntity.getVerNum()) {
            WaLogger.logger.info("有最新版本");
            result.put("MsgCode",MsgCode);
            result.put("MsgDesc","发现最新版本");
            result.put("downUrl",appVersionEntity.getVersionUrl());
        } else {
            result.put("MsgCode",MsgCode);
            result.put("MsgDesc","当前为最新版本");
            result.put("downUrl",null);
        }
        return result;
    }

    //接口62
    @Cacheable(value = "DownloadLogoFile-key")
    public List<Map<String,Object>> DownloadLogoFile(String username) {
        Map map = new HashMap();
        List<Map<String,Object>> comList = new ArrayList<>();
        CompanyEntity companyLog =  companyRepository.findByUsername(username);
        if(companyLog == null ||  companyLog.getLogo() == null){
            map.put("logo","/files/logo/default.png");
        }else{
            map.put("log",companyLog.getLogo());

        }
        comList.add(map);
        return comList;
    }

    /**
     * 通过版本号查找apk
     * @param versionNum
     * @return
     */
    public String DownloadApkByVersionNum(Long versionNum,String identify) {
        String url = "";
        AppVersionEntity appVersionEntity =  appversionRepository.findByVerNumAndIdentify(versionNum,identify);
        if(appVersionEntity == null ||  appVersionEntity.getVersionUrl() == null){
            return url;
        }
        url = URL_BASE + appVersionEntity.getVersionUrl();

        return url;
    }

    /**
     *
     */
    public String DownloadNewestApk() {
        AppVersionEntity appVersionEntity = appversionRepository.findNewestApk();
        if (appVersionEntity == null || appVersionEntity.getVersionUrl() == null) {
            return "";
        }
        return URL_BASE + appVersionEntity.getVersionUrl();
    }

    public String DownloadNewestApkByIdentify(String identify) {
        AppVersionEntity appVersionEntity = appversionRepository.findNewestApkByIdentify(identify);
        if (appVersionEntity == null || appVersionEntity.getVersionUrl() == null) {
            return "";
        }
        return URL_BASE + appVersionEntity.getVersionUrl();
    }

    public HashMap<String,String> getDownloadPictureIdentify(String identify) {

        HashMap<String,String> map = new HashMap<>();

        DownloadPictureEntity downloadAndroidPictureEntity = downloadPictureRepository.findByIdentifyAndType(identify,"ios");
        DownloadPictureEntity downloadIosPictureEntity = downloadPictureRepository.findByIdentifyAndType(identify,"android");
        if (downloadAndroidPictureEntity == null || downloadIosPictureEntity.getUrl() == null) {
            map.put("android","");
        }else{
            map.put("android",URL_BASE +"/" + downloadAndroidPictureEntity.getUrl());
        }
        if (downloadIosPictureEntity == null || downloadIosPictureEntity.getUrl() == null) {
            map.put("ios","");
        }else{
            map.put("ios",URL_BASE +"/" + downloadIosPictureEntity.getUrl());
        }

        return map;
    }
}
